<?php

if (!defined('IN_LEKO')) {header("HTTP/1.0 403 Forbidden");exit;}

class Storage
{
	public $db_connect_id;
	public $query_result;//子类访问
	private $return_on_error = false;
	private $transaction = false;
	private $sql_time = 0;
	private $num_queries = 0;
	private $open_queries = array();

	private $curtime = 0;
	private $query_hold = '';
	private $html_hold = '';
	private $sql_report = '';
	private $cache_num_queries = 0;
	
	private $dbname = '';

	public function setReturnError($fail = false)
	{
		$this->return_on_error = $fail;
	}

	public function getNumQueries()
	{
		return $this->num_queries;
	}

	function dbClose()
	{
		if (!$this->db_connect_id)
		{
			return false;
		}

		if ($this->transaction)
		{
			$this->sql_transaction('commit');
		}

		if (sizeof($this->open_queries))
		{
			foreach ($this->open_queries as $i_query_id => $query_id)
			{
				$this->Freeresult($query_id);
			}
		}
		
		return $this->_dbClose();
	}

	function fetchRowset($query_id = false)
	{
		if (!$query_id)
		{
			$query_id = $this->query_result;
		}

		if ($query_id)
		{
			$result = array();

			while ($row = $this->fetchRow($query_id))
			{
				$result[] = $row;
			}

			return $result;
		}
		
		return false;
	}

	function dbError($sql = '')
	{
		$error = $this->_dbError();

		if (!$this->return_on_error)
		{
			$this_page = (isset($_SERVER['PHP_SELF']) && !empty($_SERVER['PHP_SELF'])) ? $_SERVER['PHP_SELF'] : $_ENV['PHP_SELF'];
			$this_page .= '&' . ((isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING'])) ? $_SERVER['QUERY_STRING'] : (isset($_ENV['QUERY_STRING']) ? $_ENV['QUERY_STRING'] : ''));

			$message = '<u>SQL ERROR</u> [ ' . SQL_LAYER . ' ]<br /><br />' . $error['message'] . ' [' . $error['code'] . ']<br /><br /><u>CALLING PAGE</u><br /><br />'  . htmlspecialchars($this_page) . (($sql != '') ? '<br /><br /><u>SQL</u><br /><br />' . $sql : '') . '<br />';

			if ($this->transaction)
			{
				$this->transAction('rollback');
			}
			
			trigger_error($message, E_USER_ERROR);
		}
		
		return $error;
	}

}

?>